﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
using Microsoft.Office.Interop.Excel; 


namespace project_forgotten_tuba
{
    public static class GameStatic
    {
        public static int WINDOW_WIDTH = 1824;
        public static int WINDOW_HEIGHT = 912;

        public static int CARD_WIDTH = 148;
        public static int CARD_HEIGHT = 208;

        public static GraphicsDevice MainGraphicsDevice { get; set; }

        public static int PlayerIndex { get; set; }

        public static Texture2D FileToTexture(string filename, string filepath = "")
        {
            FileStream tempstream = new FileStream(filepath + filename, FileMode.Open);
            Texture2D j = Texture2D.FromStream(GameStatic.MainGraphicsDevice, tempstream);
            tempstream.Flush();
            tempstream.Close();
            return j;
        }
        public static SpriteFont GameFont { get; set; }







        private static Microsoft.Office.Interop.Excel.Application appExcel;
        private static Workbook newWorkbook = null;
        private static _Worksheet objsheet = null;
        public static bool excel_loaded = false;

        //Method to initialize opening Excel
        public static void excel_init(String path)
        {
            try
            {
                appExcel = new Microsoft.Office.Interop.Excel.Application();
                excel_loaded = true;
            }
            catch (Exception e)
            {
                excel_loaded = false;
                return;
            }

            if (System.IO.File.Exists(path))
            {
                // then go and load this into excel
                var spreadsheetLocation = Path.Combine(Directory.GetCurrentDirectory(), path);
                newWorkbook = appExcel.Workbooks.Open(spreadsheetLocation, true, true);
                objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
            }
            else
            {
                Console.WriteLine("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                Environment.Exit(0);
            }
        }

        //Method to get value; cellname is A1,A2, or B1,B2 etc...in excel.
        public static string excel_getValue(string cellname)
        {
            string value = string.Empty;
            try
            {
                value = objsheet.get_Range(cellname).get_Value().ToString();
            }
            catch
            {
                value = "";
            }

            return value; 
        }

        //Method to close excel connection
        public static void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkbook.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objsheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    Console.WriteLine("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }









  
    }
 


}
